<h2>Акция</h2> <br/>
<div class="scrollable">
	<div class="tabulation" data-id="tabs-1">
		<a class="element active" data-id="tab-1">Основное</a>
		<? $tab_num = 2; ?>
		<? foreach( $this->langs as $l => $lang ): ?>
			<a class="element" data-id="tab-<?=$tab_num++?>"><?=Yii::t('lang',$lang)?></a>
		<? endforeach; ?>
		<a class="element" data-id="tab-<?=$tab_num++?>">Изображение</a>
	</div>
	<form method="post" action="<?= CHtml::normalizeUrl(array('catalog/SaveAction', 'id' => isset($item) ? $item->id : 'new')) ?>" class="modal">
		<div class="tabs" id="tabs-1">
			<div class="tab active" id="tab-1">
				<ul class="switch-box">
					<li>
						<div><strong>Видимость:</strong></div>
						<div>
							<select class="switch" name="item[isVisible]">
								<option value="0" <?= isset($item) ? ($item->isVisible == "0") ? " selected" : "" : '' ?>>1</option>
								<option value="1" <?= isset($item) ? ($item->isVisible == "1") ? " selected" : "" : 'selected' ?>>0</option>
							</select>
						</div>
					</li>
				</ul>
				<table class="step-right">
					<tr>
						<td><strong>Дата начала:</strong></td>
						<td><input class="input wide" name="item[date_start]" type="date" required id="date" value="<?= isset($item) ? $item->date_start : '' ?>"></td>
						<td></td>
					</tr>
					<tr>
						<td><strong>Дата окончания:</strong></td>
						<td><input class="input wide" name="item[date_end]" type="date" required id="date" value="<?= isset($item) ? $item->date_end : '' ?>"></td>
						<td></td>
					</tr>
				</table>
			</div>
			<? $tab_num =2; ?>
			<? foreach( $this->langs as $l => $lang ):
				if( $l === Yii::app()->params['defaultLanguage'] ) {
					$suffix = '';
				} else {
					$suffix = '_'.$l;
				}
				?>
				<div class="tab" id="tab-<?=$tab_num++?>">
					<table class="step-right">
						<tr>
							<td><strong>Название:</strong></td>
							<td><input class="input wide" name="item[title<?=$suffix?>]" type="text" id="name" value="<?=isset($item)?($suffix=='')?$item->title:$item->getLangAttribute('title_' . $l):''?>"></td>
						</tr>
						<tr>
							<td><strong>Краткое описание:</strong></td>
							<td></td>
						</tr>
						<tr>
							<td colspan="3">
								<textarea name="item[small<?=$suffix?>]" class="input wide"><?=isset($item)?($suffix=='')?$item->small:$item->getLangAttribute('small_' . $l):''?></textarea>
							</td>
						</tr>
						<tr>
							<td><strong>Полное описание:</strong></td>
							<td></td>
						</tr>
						<tr>
							<td colspan="3">
								<textarea name="item[body<?=$suffix?>]" class="input wide"><?=isset($item)?($suffix=='')?$item->body:$item->getLangAttribute('body_' . $l):''?></textarea>
							</td>
						</tr>
					</table>
				</div>
			<?php endforeach; ?>
			<div class="tab" id="tab-<?=$tab_num++?>">
				<div class="step-right">
					<? if (isset($item->img)): ?>
						<strong>Изображения:</strong>
						<div class="images images-sortable">
							<div class="image">
								<div class="icon100">
									<img src="<?= $this->baseUrl . $item->img ?>" class="round100">
								</div>
								<div class="image-remove">
									<a class="btn-red delete-image" data-id="<?= $item->id ?>">Удалить</a>
								</div>
								<input type="hidden" name="images[<?= $item->id ?>][is_deleted]" value="0" id="d-<?= $item->id ?>">
							</div>
						</div>
					<? endif ?>

					<h4>Добавить изображения:</h4>

					<div class="image-drop dropzone"></div>
					<input class="input" name="item[img]" type="hidden" id="img" value="<?= isset($item->img) ? $this->baseUrl . $item->img : '' ?>">
					<input class="input" name="imgs_url" type="hidden" id="imgs_url" required>
				</div>
			</div>
			<input type="submit" value="Сохранить" class="btn-green save-btn" id="submit">
		</div>
	</form>
</div>
<script type="text/javascript">
	$(function() {
		<? foreach( $this->langs as $l => $lang ):
		if( $l === Yii::app()->params['defaultLanguage'] ) {
			$suffix = '';
		} else {
			$suffix = '_'.$l;
		}
		?>
		var editor<?=$suffix?>;
		editor<?=$suffix?> = CKEDITOR.replace( 'item[body<?=$suffix?>]',
			{
				filebrowserBrowseUrl :'/instinct/js/ckeditor/filemanager/browser/default/browser.html?Connector=/instinct/js/ckeditor/filemanager/connectors/php/connector.php',
				filebrowserImageBrowseUrl : '/instinct/js/ckeditor/filemanager/browser/default/browser.html?Type=Image&Connector=/instinct/js/ckeditor/filemanager/connectors/php/connector.php',
				filebrowserFlashBrowseUrl :'/instinct/js/ckeditor/filemanager/browser/default/browser.html?Type=Flash&Connector=/instinct/js/ckeditor/filemanager/connectors/php/connector.php',
				filebrowserUploadUrl  :'/instinct/js/ckeditor/filemanager/connectors/php/upload.php?Type=File',
				filebrowserImageUploadUrl : '/instinct/js/ckeditor/filemanager/connectors/php/upload.php?Type=Image',
				filebrowserFlashUploadUrl : '/instinct/js/ckeditor/filemanager/connectors/php/upload.php?Type=Flash',
				width: '90%'
			});

		<? endforeach; ?>
		$('.popup-outer').click(function() {
			for(name in CKEDITOR.instances)
			{
				CKEDITOR.instances[name].destroy()
			}

		});
		var num = 0;
		var switchID = 0;
		$('.switch', '.modal').each(function () {
			$(this).attr('data-id', switchID);
			temp = "";
			leftSpace = "";
			if ($(this).val() == 1) {
				temp = " true";
				leftSpace = "style='left: 1em;'"
			}
			;
			$(this).after("<div class='switcher" + temp + "' data-id='" + switchID + "' data-state='1'><div class='toggle' " + leftSpace + "></div></div>");
			$(this).hide();

			switchID++;
		});
		$('.tabulation .element').click(function () {
			$('.tabulation .element').removeClass('active');
			$(this).addClass('active');
			$('#' + $(this).parent().data('id') + ' > .tab').hide();
			$('#' + $(this).data('id')).show();

		});
		$('.switcher', '.modal').click(function () {
			$(this).toggleClass('true');


			if ($(this).hasClass('true')) {
				$('.toggle', $(this)).animate({'left': '1em'}, 200);
				$('.switch[data-id="' + $(this).data('id') + '"]').val(1);
			} else {
				$('.toggle', $(this)).animate({'left': '0em'}, 200);
				$('.switch[data-id="' + $(this).data('id') + '"]').val(0);
			}
			console.log($('.switch[data-id="' + $(this).data('id') + '"]').val());
		});

		$('.autofill').keyup(function () {
			var count = 0;
			var current_value = $(this).val();
			$('#av-' + $(this).attr('id')).children().each(function () {
				if ($(this).html().toLowerCase().indexOf(current_value.toLowerCase()) != -1) {
					$(this).show();
					count++;
				} else {
					$(this).hide();
				}
			});
			if (count > 0) {
				$('#av-' + $(this).attr('id')).show();
			}
			;
		});
		$(".autofill-values > .element").click(function () {
			$("#" + $(this).data('id')).val($(this).html());
			$(this).parent().hide();
		});
		$('html').click(function () {
			$('.autofill-values').hide();
			console.log('ok');
		});
		$('.autofill-holder').click(function (e) {
			e.stopPropagation();
		});
	});


	$('.delete-image').click(function () {
		$("#d-" + $(this).data('id')).val(1);
		$(this).parent().parent().fadeOut(400);
	});

	$('.autofill').keyup(function () {
		var count = 0;
		var current_value = $(this).val();
		$('#av-' + $(this).attr('id')).children().each(function () {
			if ($(this).html().toLowerCase().indexOf(current_value.toLowerCase()) != -1) {
				$(this).show();
				count++;
			} else {
				$(this).hide();
			}
		});
		if (count > 0) {
			$('#av-' + $(this).attr('id')).show();
		}
		;
	});
	$(".autofill-values > .element").click(function () {
		$("#" + $(this).data('id')).val($(this).html());
		$(this).parent().hide();
	});
	$('html').click(function () {
		$('.autofill-values').hide();
		console.log('ok');
	});
	$('.autofill-holder').click(function (e) {
		e.stopPropagation();
	});
	var newFileNames = {};
	$("div.image-drop").dropzone({
		maxFiles: 1,
		url: "<?= CHtml::normalizeUrl(array('function/uploadIcon', 'f'=>'actions'))?>",
		addRemoveLinks: true,
		uploadMultiple: false,
		success: function (data, response) {
			$("#img").val(response);
			$("#imgs_url").val($("#imgs_url").val() + ',' + response);
			newFileNames[data.name] = response;
		},
		maxFilesize: 3,
		init: function () {
			var dz = this
			this.on("removedfile", function (data) {
				$.ajax({
					url:'<?=CHtml::normalizeUrl(array('function/DeletedImage'))?>',
					type:'GET',
					data:{img:newFileNames[data.name]},
					success: function () {
						humane.log('Изображение успешно удалено');
					},
					error:function(){
						humane.log('Ошибка');
					}
				});
				delete newFileNames[data.name];
				$("#img_urls").val("");
				f = $("#img").val();
				for (var key in newFileNames) {
					$("#img_urls").val($("#img_urls").val() + ',' + newFileNames[key]);
					$("#img_url").val(newFileNames[key]);
				}
				if (f == $("#img").val()) $("#img_url").val("");
			});
		}
	});

	// $("div.images-drop").dropzone();
</script>
